package com.sf.manager.impl;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.util.ArrayList;
import java.util.List;

//@Component
public class OperateLogConsumer implements Runnable {

	private static final Logger logger = LoggerFactory.getLogger(OperateLogConsumer.class);
	public static final int DEFAULT_BATCH_SIZE = 64;
	@Autowired
	private OperateLogQueue taskLogQueue;
	@Autowired
	//private TaskOperateLogMapper taskOperateLogMapper;
	private int batchSize = DEFAULT_BATCH_SIZE;
	private boolean active = true;

	@PostConstruct
	public void init() {
		Thread thread = new Thread(this);
		thread.start();
	}

	@PreDestroy
	public void close() {
		active = false;
	}

	@Override
	public void run() {
		while (active) {
			try {
				execute();
			} catch (Exception e) {
				logger.error("write task operator log error:", e);
			}
		}
	}

	public void execute() {
	/*	List<TaskOperateLog> taskLogs = new ArrayList<>();

		try {
			int size = 0;

			while (size < batchSize) {
				TaskOperateLog taskLog = taskLogQueue.poll();

				if (taskLog == null) {
					break;
				}

				taskLogs.add(taskLog);
				size++;
			}
		} catch (Exception ex) {
			logger.info(ex.getMessage(), ex);
		}

		if (!taskLogs.isEmpty()) {
			taskOperateLogMapper.batchInsert(taskLogs);
		}*/
	}

}
